(function () {
    'use strict';

    var setup = function (editor, url) {
      var articleData = {};
      var isHasRequest = false;
      var pluginName = '\u9009\u62e9\u6587\u7ae0';
      var getArticles = function () {
        $.ajax({
          type: 'get',
          url: '/mock/articles.json',
          dataType: 'json'
        }).done(function (res) {
          articleData = res.data;
          openAddArticle(articleDomInit());
        }).fail(function (err) {
        });
      };
      var articleDomInit = function () {
        var tabsStructure = [];
        var _loop_1 = function (key) {
          if (articleData.hasOwnProperty(key)) {
            var itemsArr_1 = [];
            var element = articleData[key];
            element.map(function (e) {
              itemsArr_1.push({
                type: 'checkbox',
                name: e.title + '|$' + e.link,
                label: e.title
              });
            });
            tabsStructure.push({
              name: key,
              title: key,
              items: itemsArr_1
            });
          }
        };
        for (var key in articleData) {
          _loop_1(key);
        }
        return tabsStructure;
      };
      var openDialog = function () {
        if (!isHasRequest) {
          isHasRequest = true;
          getArticles();
        } else {
          openAddArticle(articleDomInit());
        }
      };
      var openAddArticle = function (tabsStructure) {
        return editor.windowManager.open({
          title: pluginName,
          body: {
            type: 'tabpanel',
            tabs: tabsStructure
          },
          buttons: [
            {
              type: 'cancel',
              text: 'Close'
            },
            {
              type: 'submit',
              text: 'Save',
              primary: true
            }
          ],
          onSubmit: function (api) {
            var data = api.getData();
            var insertHtml = '';
            for (var key in data) {
              if (data.hasOwnProperty(key)) {
                if (data[key]) {
                  var arr = key.split('|$');
                  insertHtml += '<div><a href="' + arr[1] + '">' + arr[0] + '</a></div>';
                }
              }
            }
            editor.insertContent(insertHtml);
            api.close();
          }
        });
      };
      editor.ui.registry.addIcon('addarticle', '<svg style="width:21px;height:21px;" t="1584424131210" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2493" width="200" height="200"><path d="M884.932677 203.143935L693.933216 10.48841a35.881402 35.881402 0 0 0-25.392992-10.48841h-552.021563A70.65876 70.65876 0 0 0 45.859901 70.65876v850.113208A70.65876 70.65876 0 0 0 116.518661 993.638814H377.072839a35.32938 35.32938 0 0 0 0-70.65876H151.848041a35.32938 35.32938 0 0 1-35.32938-39.745553V105.98814a35.32938 35.32938 0 0 1 35.32938-35.32938h425.056604v177.198922a70.65876 70.65876 0 0 0 70.65876 70.65876h177.198922v173.334771a35.32938 35.32938 0 1 0 70.65876 0V227.984906a35.32938 35.32938 0 0 0-10.48841-24.840971z m-201.487871 44.713747a35.32938 35.32938 0 0 1-35.32938-35.32938V70.65876l176.646901 177.198922z" fill="#333333" p-id="2494"></path><path d="M597.881464 449.897574H221.954779a35.32938 35.32938 0 0 0 0 70.65876H597.881464a35.32938 35.32938 0 0 0 0-70.65876zM356.096019 651.937466H232.443189a35.32938 35.32938 0 0 0 0 70.65876h123.65283a35.32938 35.32938 0 0 0 0-70.65876zM221.954779 318.516442H432.274995a35.32938 35.32938 0 1 0 0-70.65876H221.954779a35.32938 35.32938 0 0 0 0 70.65876z" fill="#333333" p-id="2495"></path><path d="M723.190359 1024m0-33.121294l0-375.374663q0-33.121294 33.121294-33.121294l0 0q33.121294 0 33.121294 33.121294l0 375.374663q0 33.121294-33.121294 33.121294l0 0q-33.121294 0-33.121294-33.121294Z" p-id="2496"></path><path d="M977.6723 836.312668m-33.121294 0l-375.374663 0q-33.121294 0-33.121294-33.121293l0 0q0-33.121294 33.121294-33.121294l375.374663 0q33.121294 0 33.121294 33.121294l0 0q0 33.121294-33.121294 33.121293Z" p-id="2497"></path></svg>');
      editor.ui.registry.addButton('addarticle', {
        tooltip: pluginName,
        icon: 'addarticle',
        onAction: function () {
          openDialog();
        }
      });
      editor.ui.registry.addMenuItem('addarticle', {
        onAction: function () {
          openDialog();
        }
      });
    };
    function Plugin () {
      tinymce.PluginManager.add('addarticle', setup);
    }

    Plugin();

}());
